package bp.zoufeng.com.bp;

import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.graphics.Color;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.RequiresApi;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;

import java.io.File;
import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {

    private VideoView video;
    private String filename;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = getWindow();
        //取消设置透明状态栏,使 ContentView 内容不再覆盖状态栏
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        //需要设置这个 flag 才能调用 setStatusBarColor 来设置状态栏颜色
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        //设置状态栏颜色
        window.setStatusBarColor(Color.parseColor("#8b0f1e"));
        setContentView(R.layout.activity_main);


    }


    public void myinfo(View view){

        String sum = ((EditText)findViewById(R.id.et_sum)).getText().toString();
        String today = ((EditText)findViewById(R.id.et_today)).getText().toString();
        String out = ((EditText)findViewById(R.id.et_out)).getText().toString();

        Intent intent = new Intent(this,MyActivity.class);
        intent.putExtra("sum",sum);
        intent.putExtra("today",today);
        intent.putExtra("out",out);
        startActivity(intent);

    }








    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    MediaPlayer mediaPlayer = new MediaPlayer();

    public void qqq(View view){
//        startmusic();
//        play();
        double i =  Math.random()*10;
        Toast.makeText(this,(int)i+"",0).show();

    }


    public void qqq2(View view){
        startVoie();
    }

    public void qqq3(View view){
        stopRecording();
    }


    private void play() {
        this.filename="www.aac";
        try {
            AssetManager assetManager = this.getAssets();   ////获得该应用的AssetManager
            AssetFileDescriptor afd = assetManager.openFd(filename);   //根据文件名找到文件
            //对mediaPlayer进行实例化
            mediaPlayer = new MediaPlayer();
            if (mediaPlayer.isPlaying()) {
                mediaPlayer.reset();    //如果正在播放，则重置为初始状态
            }
            mediaPlayer.setDataSource(afd.getFileDescriptor(),
                    afd.getStartOffset(), afd.getLength());     //设置资源目录
            mediaPlayer.prepare();//缓冲
            mediaPlayer.start();//开始或恢复播放
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    private void startmusic() {


        //1 初始化mediaplayer
        final MediaPlayer mediaPlayer = new MediaPlayer();
        //2 设置到播放的资源位置 path 可以是网络 路径 也可以是本地路径

        try {
            mediaPlayer.setDataSource("http://www.w3school.com.cn/i/song.mp3");
            //3 准备播放
            mediaPlayer.prepareAsync();
            //3.1 设置一个准备完成的监听
            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    // 4 开始播放
                    mediaPlayer.start();
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }

//
//        String nowMusic="http://www.w3school.com.cn/i/song.mp3";
////        String nowMusic="https://imhdfs3.dccnet.com.cn/userfiles/public/static/bb08fa43404b6ba10e674fc55a0c0f9a7bbfc946.aac";
////        String nowMusic="http://sc1.111ttt.cn:8282/2018/1/03m/13/396131229550.m4a";
////        String nowMusic="http://122.20.221.99/userfiles/public/static/aa5a21930528e92dd5d1047416cee0cb9ddf18a1.aac";
////        String nowMusic="https://imhdfs3.dccnet.com.cn/userfiles/public/static/8d7909c5e7a8dad9719fe6832c2642eb10301fc7.aac";
//        try {
//
//            mediaPlayer = new MediaPlayer();
//
////            mediaPlayer.setDataSource(this, Uri.parse(nowMusic));//设置播放的数据源。
//            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
//            mediaPlayer.setDataSource(nowMusic);//设置播放的数据源。
//            //mediaPlayer.prepare();//同步的准备方法。
//            mediaPlayer.prepareAsync();//异步的准备
//            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
//                @Override
//                public void onPrepared(MediaPlayer mp) {
//                    mp.start();
//                    Toast.makeText(MainActivity.this, "播放开始", Toast.LENGTH_SHORT).show();
//                }
//            });
//            mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
//                @Override
//                public void onCompletion(MediaPlayer mp) {
//                    Toast.makeText(MainActivity.this, "播放结束", Toast.LENGTH_SHORT).show();
//                    Log.i("aaa","qqq");
//                }
//            });
//
//        } catch (Exception e) {
//            e.printStackTrace();
//            Toast.makeText(this, "播放失败", Toast.LENGTH_SHORT).show();
//        }

    }




    private MediaRecorder mMediaRecorder = null;
    private String voicePath = "";
    private Timer recorderTimer;

    /**
     * 曾岩同学 10月代码同步11月版本
     */
    //    开启录音
    public void startVoie() {
        File voiceFilePath = new File(Environment.getExternalStorageDirectory().toString() + File.separator + "111aaa" + File.separator);
        Toast.makeText(this,voiceFilePath.toString(),0).show();
        if (!voiceFilePath.exists()) {
            voiceFilePath.mkdirs();
        }
        if (mMediaRecorder == null) {
            mMediaRecorder = new MediaRecorder();
            mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.AAC_ADTS); //录音文件保存的格式，这里保存为 aac
            mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
            mMediaRecorder.setAudioChannels(1);
            // 设置录音文件的清晰度
            mMediaRecorder.setAudioSamplingRate(44100);
            mMediaRecorder.setAudioEncodingBitRate(64);
        }
        voicePath = Environment.getExternalStorageDirectory().toString() + File.separator + "111aaa" + File.separator + String.valueOf(System.currentTimeMillis()) + ".aac";
        mMediaRecorder.setOutputFile(voicePath); // 设置录音文件的保存路径
        new Thread(new Runnable() {
            @Override
            public void run() {
                if (mMediaRecorder != null) {
                    try {
                        mMediaRecorder.prepare();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    mMediaRecorder.start();
                }
            }
        }).start();
    }

    // 停止录音
    protected void stopRecording() {
        if (recorderTimer != null) {
            recorderTimer.cancel();
            recorderTimer = null;
        }
        if (mMediaRecorder != null) {
            try {
                mMediaRecorder.stop(); // 停止录音
                mMediaRecorder.release();
            } catch (Exception e) {
                e.printStackTrace();
                mMediaRecorder = null;
            }
        }
        mMediaRecorder = null;
    }
}
